<?php


namespace app\common\dao;


use app\admin\model\ChatLog as ChatLogModel;
use think\Db;

class ChatLogDao extends ChatLogModel
{
    /**
     * 查询存在于聊天表中的所有聊天中的最后一条数据集合
     * @param $chatIds
     * @return ChatLogModel
     */
    public static function getInChatLastDataQuery($chatIds, $queryData = [])
    {
        $send_to_sys = ChatLogModel::SEND_TO_SYS;//系统
        $chatIds = implode(',', $chatIds); //分割为原生可以查询的语句
        return ChatLogModel::alias('cl')
            ->join(Db::raw("(
            SELECT MAX(id) AS max_id FROM ic_chat_log
  WHERE chat_id IN ($chatIds) and  send_to <> '$send_to_sys'
  GROUP BY chat_id
  ) as cl2"), "cl.id=cl2.max_id")
            ->where(function ($query) use ($queryData) {
                $send_to = $queryData['send_to'] ?? "";
                $staff_id = $queryData['staff_id'] ?? "";
                if (!empty($send_to)) $query->where('send_to', $send_to);
                if (!empty($staff_id)) $query->where('staff_id', $staff_id);
            });
    }
}